<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../../common/base_body_header.jsp" %>
<%@include file="../../common/navbar.jsp" %>
<%@include file="../../common/page_content_pre.jsp" %>

<div class="page-content">

    <div class="row">
        <div class="col-sm-12">
            <div class="widget-box">
                <div class="widget-header">
                    <h4>${not empty sysUser.userId?'修改':'添加'}用户</h4>
                </div>

                <div class="widget-body">
                    <div class="widget-main no-padding">
                        <form class="form-horizontal" modelAttribute="sysUser" id="sysUserForm">
                            
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">姓名：</label>
 									<input id="userId" name="userId" type="hidden"
                                               value="${sysUser.userId}">
                                    <div class="col-sm-5">
                                        <input name="username" htmlEscape="false" maxlength="50"
                                                    class="input-xlarge required" placeholder="请输入用户名" value="${sysUser.username}"/>
                                        <span class="help-inline"><font color="red">*</font> </span>
                                    </div>
                                </div>
                            </fieldset>

                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">手机号：</label>

                                    <div class="col-sm-4">
                                        <input name="account" htmlEscape="false" maxlength="50"
                                                    class="input-xlarge required" placeholder="请输入用户账户" value="${sysUser.account}"/>
                                        <span class="help-inline"><font color="red">*</font> </span>
                                    </div>
                                </div>
                            </fieldset>

                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">密码：</label>

                                    <div class="col-sm-6">
                                        <input class="input-xlarge" id="s_pwd"
                                               name="password" type="password" maxlength="50" minlength="3"
                                               placeholder="请输入密码"/>
                                        <c:if test="${not empty sysUser.userId}"><span
                                                class="help-inline">若不修改密码，请留空。</span></c:if>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">确认密码：</label>

                                    <div class="col-sm-5">
                                        <input class="input-xlarge" id="s_repeat_pwd" name="repeatPwd" type="password"
                                               value="" maxlength="50" minlength="3" placeholder="请输入确认密码"
                                               equalTo="#s_pwd"/>
                                        <c:if test="${empty sysUser.userId}"><span class="help-inline"><font
                                                color="red">*</font> </span></c:if>
                                    </div>
                                </div>
                            </fieldset>

                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">地址：</label>

                                    <div class="col-sm-4">
                                        <input name="address" htmlEscape="false" maxlength="100"
                                                    class="input-xlarge required" placeholder="请输入用户地址" value="${sysUser.address}"/>
                                        <span class="help-inline"><font color="red">*</font> </span>
                                    </div>
                                </div>
                            </fieldset>

                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">销售管理：</label>

                                    <div class="col-sm-4">
                                        <select name="salesperson" class="form-control">
                                           <c:if test="${not empty salesUser}">
                                               <c:forEach items="${allSales}" var="sales" varStatus="status">
											       <option value="${sales.userId}" <c:if test="${salesUser.userId==sales.userId}">selected</c:if>>${sales.username}-${sales.account}</option>
											   </c:forEach>
	 										</c:if>
	 										
	 										<c:if test="${empty salesUser}">
	 										   <option value="">暂未分配</option>
	 										   <c:forEach items="${allSales}" var="sales" varStatus="status">
											       <option value="${sales.userId}">${sales.username}-${sales.account}</option>
											   </c:forEach>
	 										</c:if>
                                        </select>
                                    </div>
                                </div>
                            </fieldset>

                            <c:if test="${not empty sysUser.userId}">
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">创建时间：</label>

                                        <div class="col-sm-6">
                                            <label class="lbl"><fmt:formatDate value="${sysUser.createDate}"
                                                                               type="both" dateStyle="full"/></label>
                                        </div>
                                    </div>
                                </fieldset>
                            </c:if>

                           

                            <div class="form-actions center">
                                <button type="button" class="btn btn-sm btn-grey" id="go_back_btn">
                                    返回
                                    <i class="icon-reply icon-on-right bigger-110"></i>
                                </button>
                                <shiro:hasPermission name='sys:user:edit'>
                                    <button type="submit" class="btn btn-sm btn-success" onclick="save();return false;">
                                        保存
                                        <i class="icon-save icon-on-right bigger-110"></i>
                                    </button>
                                </shiro:hasPermission>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>

</div>

<%@include file="../../common/page_content_suf.jsp" %>
<%@include file="../../common/script.jsp" %>
<%@include file="../../common/treeview.jsp" %>

<script type="text/javascript">
   
	//提交事件
	var save = function () {
		var username=$("input[name='username']").val().trim();
        if(username.length==0){
        	 layer.alert("请输入姓名", {icon: 1, title: '提示', btn: ['OK']});
        	 return;
        }
        var account=$("input[name='account']").val().trim();
        if(account.length==0){
        	 layer.alert("请输入手机号", {icon: 1, title: '提示', btn: ['OK']});
        	 return;
        }
        var password=$("input[name='password']").val().trim();
        var repeatPwd=$("input[name='repeatPwd']").val().trim();
        if(password.length>0){
        	if(password!=repeatPwd){
        		 layer.alert("两次密码输入不一致", {icon: 1, title: '提示', btn: ['OK']});
            	 return;
        	}
        }
        var address=$("input[name='address']").val().trim();
        if(address.length==0){
        	 layer.alert("请输入地址", {icon: 1, title: '提示', btn: ['OK']});
        	 return;
        }
        
		layer.load();
        $("#sysUserForm").ajaxSubmit({
			   url: "${GLOBAL.basePath}/sysuser/save",
			   type:"POST",
			   success:function(data){
				if(data.code==-1){
						   layer.alert(data.msg, {icon: 1, title: '提示', btn: ['OK']}); 
			    }else{
						   layer.alert(data.msg, {icon: 1, title: '提示', btn: ['OK']}, function () {
							   document.location.href = "${GLOBAL.basePath}/sysuser/list";
						   });
				}
				layer.closeAll('loading');
			    return false; //防止页面跳转
			   }
			 
		});
		
	};
	
	
    $(document).ready(function () {
        $("#mobile").focus();
        $("#sysUserForm").validate({
            rules: {
                mobile: {
                    remote: {
                        type: "POST",
                        cache: false,
                        url: "${GLOBAL.basePath}/sysuser/checkAccount",
                        data: {
                            oldAccount: function () {
                                return "${sysUser.account}";
                            }
                        }
                    }
                }
            },
            messages: {
                mobile: {required: "用户登录名不能为空", remote: jQuery.format("用户登录名已存在")},
                confirmNewPassword: {equalTo: "输入与上面相同的密码"}
            },
            submitHandler: function (form) {
                var ids = [], nodes = tree.getCheckedNodes(true);
                for (var i = 0; i < nodes.length; i++) {
                    ids.push(nodes[i].id);
                }
                $("#menuIds").val(ids);
                loading('正在提交，请稍等...');
                form.submit();
            },
            errorContainer: "#messageBox",
            errorPlacement: function (error, element) {
                $("#messageBox").text("输入有误，请先更正。");
                if (element.is(":checkbox") || element.is(":radio") || element.parent().is(".input-append")) {
                    error.appendTo(element.parent().parent());
                } else {
                    error.insertAfter(element);
                }
            }
        });

        $("#go_back_btn").on("click", function () {
            goBack();
        });
    });

    function goBack() {
        document.location.href = "${GLOBAL.basePath}/sysuser/list";
    }
</script>
